﻿   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Collections;
using OME.Storage;

namespace OME
{
   class Generator
   {
      string[] tempTraders = {"DCPU", "COMP", "GENR", "TEMP", "TRDR", "CBOT", "NASQ", "CBOE", "RNDM", "JOLY" };
      string traderAck;
      double price;
      double offerPrice;
      int startPrice;
      int quantity;      
      int  BuySell;
      Random rdm;

      public Generator()
      {
         rdm = new Random();
      }

      public Order generateOrder(string service)
      {

         //do not randomize
         string type = "ERROR";
         string BS = "ERROR";
         string option = "NO"; //everything is futures, no options
         string orderType = "Market"; //just a market order for now.  now other order types
         
         if (service.Contains("ORDR"))
         {
            int remove = service.Length - 4;
            service = service.Remove(remove);
            return ConstructOrder(service);
         }
         else if (service.Equals("TRAN"))
         {
            type = service;
            startPrice = 710;
            price = rdm.Next(startPrice-5, startPrice+5);
            price += Math.Round(rdm.NextDouble(), 2);
            quantity = rdm.Next(1, 100);
         }
         else if (service.Equals("LEGL"))
         {
            type = service;
            startPrice = 343;
            price = rdm.Next(startPrice - 2, startPrice + 2);
            price += Math.Round(rdm.NextDouble(), 2);
            quantity = rdm.Next(1, 100);
         }
         else if (service.Equals("WELL"))
         {
            type = service;
            startPrice = 6410;
            price = rdm.Next(startPrice - 15, startPrice + 15);
            price += Math.Round(rdm.NextDouble(), 2);
            quantity = rdm.Next(1, 100);
         }
         else if (service.Equals("ADMN"))
         {
            type = service;
            startPrice = 176;
            price = rdm.Next(startPrice - 1, startPrice + 1);
            price += Math.Round(rdm.NextDouble(), 2);
            quantity = rdm.Next(1, 100);
         }
         else if (service.Equals("HEAL"))
         {
            type = service;
            startPrice = 374;
            price = rdm.Next(startPrice - 3, startPrice + 3);
            price += Math.Round(rdm.NextDouble(), 2);
            quantity = rdm.Next(1, 100);
         }
         else if (service.Equals("UTIL"))
         {
            type = service;
            startPrice = 345;
            price = rdm.Next(startPrice - 4, startPrice + 4);
            price += Math.Round(rdm.NextDouble(), 2);
            quantity = rdm.Next(1, 100);
         }
         else if (service.Equals("SECR"))
         {
            type = service;
            startPrice = 356;
            price = rdm.Next(startPrice - 4, startPrice + 4);
            price += Math.Round(rdm.NextDouble(), 2);
            quantity = rdm.Next(1, 100);
         }
         else if (service.Equals("FIRE"))
         {
            type = service;
            startPrice = 325;
            price = rdm.Next(startPrice - 2, startPrice + 2);
            price += Math.Round(rdm.NextDouble(), 2);
            quantity = rdm.Next(1, 100);
         }

         int ack = rdm.Next(10);
         traderAck = tempTraders[ack];

         double dub = rdm.NextDouble();
         if (dub >= 0.0 && dub < 0.5)
            BS = "B";
         else if (dub >= 0.5)
            BS = "S";

         return new OME.Storage.ServiceOrder(traderAck, type, orderType, BS, price, quantity);
      }

      private ServiceOrder BuildOrder(string[] mktOrder)
      {
         string id = mktOrder[0];
         string inst = mktOrder[1];
         string type = mktOrder[2];
         string bs = mktOrder[3];
         int quant = Int32.Parse(mktOrder[4]);
         double price = Double.Parse(mktOrder[5]);

         return new OME.Storage.ServiceOrder(id, inst, type, bs, price, quant);
      }

      private ServiceOrder ConstructOrder(string mktOrder)
      {
         string[] orderString = new string[6];
         int mark = 0;
         string part = "";
         for (int i = 0; i < mktOrder.Length; i++)
         {

            if (!mktOrder.ElementAt(i).Equals('|'))
            {
               part += mktOrder.ElementAt(i);
            }
            else
            {
               orderString[mark] = part;
               mark++;
               part = "";
            }

         }

         orderString[mark] = part;
         return BuildOrder(orderString);
      }
   }
}

